package com.stox.ui.javafx.navigator.view.tab.scrip;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.control.SelectionMode;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import com.stox.client.CountryClient;
import com.stox.client.ExchangeClient;
import com.stox.client.ScripClient;
import com.stox.event.AbstractModelSelectionEvent;
import com.stox.model.AbstractModel;
import com.stox.model.Country;
import com.stox.ui.javafx.core.util.MessageSource;
import com.stox.ui.javafx.core.widget.wrapper.TreeItem;
import com.stox.ui.javafx.core.widget.wrapper.TreeView;
import com.stox.ui.javafx.navigator.model.AbstractModelTreeItem;
import com.stox.ui.javafx.navigator.model.CountryTreeItem;
import com.stox.ui.javafx.navigator.view.AbstractNavigatorTab;
import com.stox.ui.javafx.navigator.widget.AutoNavigatorPanel;
import com.stox.ui.javafx.navigator.widget.SearchPanel;

@Component
public class ScripNavigatorTab extends AbstractNavigatorTab implements
		InitializingBean {

	@Autowired
	private CountryClient countryClient;
	
	@Autowired
	private ExchangeClient exchangeClient;
	
	@Autowired
	private ScripClient scripClient;
	
	@Autowired
	private ApplicationEventPublisher applicationEventPublisher;

	@Autowired
	private ScripNavigatorTabContextMenu scripNavigatorTabContextMenu;
	
	private TreeView<String> treeView = new TreeView<>();

	public ScripNavigatorTab() {
		super(MessageSource.getMessage("Scrips"));
	}
	
	public TreeView<String> getTreeView(){
		return treeView;
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		setOnSelectionChanged(createTabSelectionHandler());
		treeView.setOnMouseClicked(createMouseEventHandler());
		treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
		treeView.setContextMenu(scripNavigatorTabContextMenu);
	}
	
	private EventHandler<Event> createTabSelectionHandler(){
		return new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				TreeItem<String> rootItem = new TreeItem<>("Countries");
				treeView.setRoot(rootItem);
				BorderPane borderPane = new BorderPane();
				borderPane.setTop(new AutoNavigatorPanel(treeView));
				borderPane.setCenter(treeView);
				borderPane.setBottom(new SearchPanel(treeView));
				setContent(borderPane);
				List<Country> countries = countryClient.findAll();
				Collections.sort(countries);
				for(Country country : countries) {
					rootItem.getChildren().add(new CountryTreeItem(country, exchangeClient, scripClient));
				}
			}
		};
	}
	
	private EventHandler<MouseEvent> createMouseEventHandler(){
		return new EventHandler<MouseEvent>() {
		    @Override
		    public void handle(MouseEvent mouseEvent) {
		        if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
		            if(mouseEvent.getClickCount() == 2){
		            	TreeItem<String> treeItem = (TreeItem<String>)treeView.getSelectionModel().getSelectedItem();
		            	if(treeItem instanceof AbstractModelTreeItem) {
		            		applicationEventPublisher.publishEvent(new AbstractModelSelectionEvent<AbstractModel>(Arrays.asList(((AbstractModelTreeItem<?>)treeItem).getModel())));
		            	}
		            }
		        }else if(mouseEvent.getButton().equals(MouseButton.SECONDARY)) {
		        	treeView.getContextMenu().show(treeView, Side.RIGHT, 0, 0);
		        }
		    }
		};
	}
	
	@Override
	public int getOrder() {
		return 0;
	}

}
